//
//  UIView+ImageRepresentation.m
//  TravelGuideMdd
//
//  Created by Song Xiaofeng on 13-8-21.
//  Copyright (c) 2013年 mafengwo.com. All rights reserved.
//

#import "UIView+ImageRepresentation.h"

@implementation UIView (ImageRepresentation)

- (UIImage *)imageRepresentation;
{
    @autoreleasepool {
        UIScreen* screen = [UIScreen mainScreen];
        CGFloat scale = 1.0;
        if ([screen respondsToSelector:@selector(scale)])
        {
            scale = [screen scale];
        }
        CGSize size = self.frame.size;
        size.width *= scale;
        size.height *= scale;
        
        UIGraphicsBeginImageContext(size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        if (context == nil)
        {
            UIGraphicsEndImageContext();
            return nil;
        }
        
        CGContextSaveGState(context);
        CGContextScaleCTM(context, scale, scale);
        [self.layer renderInContext:context];
        CGContextRestoreGState(context);
        UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        if (scale > 1)
        {
            viewImage = [[UIImage alloc] initWithCGImage:viewImage.CGImage scale:scale orientation:UIImageOrientationUp];
        }
        else
        {
            viewImage = [[UIImage alloc] initWithCGImage:viewImage.CGImage];
        }
        return viewImage;
    }
}
@end
